﻿using System;
using System.Data;
using System.Collections;
using System.Runtime.Serialization;
using System.Reflection;
using System.Xml;
using OutSystems.ObjectKeys;
using OutSystems.RuntimeCommon;
using OutSystems.HubEdition.RuntimePlatform;
using OutSystems.HubEdition.RuntimePlatform.Db;

namespace OutSystems.NssRemotingRadarsAdapter {

	/// <summary>
	/// RecordList type <code>RLEvento_RRRecordList</code> that represents a record list of
	///  <code>Evento_RR</code>
	/// </summary>
	[Serializable()]
	public partial class RLEvento_RRRecordList: RecordList, IEnumerable, IEnumerator, ISerializable {
		public static void EnsureInitialized() {}

		public delegate void ReadDBMethodDelegate(ref RCEvento_RRRecord rec, IDataReader r);

		private ReadDBMethodDelegate alternateReadDBMethod;

		/// <summary>
		/// Current Record variable
		/// </summary>
		public RCEvento_RRRecord CurrentRec;

		public override void Clear() {
			base.Clear();
			CurrentRec = new RCEvento_RRRecord("");
		}
		/// <summary>
		/// Sets the reader
		/// </summary>
		public override IDataReader Reader {
			set {
				base.Reader = value;
				if(!_empty)
				Read();
			}
		}

		/// <summary>
		/// Default Constructor
		/// </summary>
		public RLEvento_RRRecordList(): this((IDbTransaction) null) {
		}

		/// <summary>
		/// Constructor with transaction parameter
		/// </summary>
		/// <param name="trans"> Transaction Parameter</param>
		public RLEvento_RRRecordList(IDbTransaction trans): base(trans) {
			CurrentRec = new RCEvento_RRRecord("");
		}

		/// <summary>
		/// Constructor with transaction parameter and alternate read method
		/// </summary>
		/// <param name="trans"> Transaction Parameter</param>
		/// <param name="alternateReadDBMethod"> Alternate Read Method</param>
		public RLEvento_RRRecordList(IDbTransaction trans, ReadDBMethodDelegate alternateReadDBMethod): this(trans) {
			this.alternateReadDBMethod = alternateReadDBMethod;
		}

		/// <summary>
		/// Constructor declaration for serialization
		/// </summary>
		/// <param name="info"> SerializationInfo</param>
		/// <param name="context"> StreamingContext</param>
		public RLEvento_RRRecordList(SerializationInfo info, StreamingContext context): base(info, context) {
			CurrentRec = (RCEvento_RRRecord) info.GetValue("CurrentRec", typeof(RCEvento_RRRecord));
		}

		/// <summary>
		/// GetObjectData method, for serialization
		/// </summary>
		/// <param name="info"> SerializationInfo</param>
		/// <param name="context"> StreamingContext</param>
		public new void GetObjectData(SerializationInfo info, StreamingContext context) {
			base.GetObjectData(info, context);
			info.AddValue("CurrentRec", (object) CurrentRec);
		}

		public override BitArray[] GetDefaultOptimizedValues() {
			BitArray[] def = new BitArray[1];
			def[0] = null;
			return def;
		}
		/// <summary>
		/// Reads next object
		/// </summary>
		public override void Read() {
			if(_reader != null) {
				if(alternateReadDBMethod == null) {
					CurrentRec.ReadDB(_reader);
					CurrentRec.AllOptimizedAttributes = AllOptimizedAttributes;
				} else {
					alternateReadDBMethod(ref CurrentRec, _reader);
					CurrentRec.AllOptimizedAttributes = GetDefaultOptimizedValues();
				}
			} else if(_data != null) {
				CurrentRec.ReadIM((RCEvento_RRRecord) _data[_currentRowNumber]);
				CurrentRec.AllOptimizedAttributes = ((RCEvento_RRRecord) _data[_currentRowNumber]).AllOptimizedAttributes;
				_saveRec = true;
				CurrentRec.RecursiveReset();
			}
		}

		/// <summary>
		/// Performs an internal recursive save
		/// </summary>
		public override void InternalRecursiveSave() {
			if((_saveRec == true) &&(_data != null)) {
				if(_currentRowNumber >= 0 && _currentRowNumber < _data.Count) {
					_data[_currentRowNumber] = CurrentRec;
				}
				_saveRec = false;
			}
			CurrentRec.InternalRecursiveSave();
		}

		/// <summary>
		/// Gets the current record
		/// </summary>
		/// <returns>The current record</returns>
		public override object Current {
			get {
				return CurrentRec; 
			}
		}

		/// <summary>
		/// Advances to next record
		/// </summary>
		/// <returns>True if exists a next record</returns>
		public override bool Advance() {
			if(base.Advance())
			Read();
			return !_eof;
		}

		/// <summary>
		/// Gets the object of a given index
		/// </summary>
		/// <param name="index"> Intex of item to get</param>
		/// <returns>The object of required index</returns>
		public RCEvento_RRRecord this[int index] {
			get {
				if(_reader != null) {
					throw new NotSupportedException("Database Record List doesn't support indexer property");
				}
				if(_data == null || index < 0 || index >= _data.Count) {
					throw new IndexOutOfRangeException("index " + index + " is out of range");
				}
				if(_data.Count == 0) {
					throw new IndexOutOfRangeException("No records exist");
				}
				if(index == _currentRowNumber) {
					Save();
					_saveRec = true;
				}
				return(RCEvento_RRRecord) _data[index];
			}
			set {
				if(_reader != null) {
					throw new NotSupportedException("Database Record List doesn't support indexer property");
				}
				if(_data == null || index < 0 || index >= _data.Count) {
					throw new IndexOutOfRangeException("index " + index + " is out of range");
				}
				if(_data.Count == 0) {
					throw new IndexOutOfRangeException("No records exist");
				}
				_data[index] = value;
				if(index == _currentRowNumber) {
					CurrentRec = (RCEvento_RRRecord) value;
				}
			}
		}

		/// <summary>
		/// Duplicates the record List
		/// </summary>
		/// <returns>The duplicated record list</returns>
		public override RecordList Duplicate() {
			if(_iterating) {
				throw new InvalidOperationException("Duplicate is not a valid operation inside a StartIteration/EndIteration block.");
			}
			RecordList t = new RLEvento_RRRecordList((IDbTransaction) null);
			t.AllOptimizedAttributes = this.AllOptimizedAttributes;
			StartIteration();
			while(!Eof) {
				t.Append(CurrentRec);
				Advance();
			}
			EndIteration();
			t.OptimizedFields = this.OptimizedFields;
			return t;
		}

		/// <summary>
		/// Performs a recursive reset
		/// </summary>
		public override void RecursiveReset() {
			Reset();
			CurrentRec.RecursiveReset();
		}

	} // RLEvento_RRRecordList
}
